package jdklearning.classloader.brokeparentdelegate;


import java.io.IOException;
import java.io.InputStream;


/**
 * @author shenenlu 2021年07月16日 上午11:52:26
 */
public class StringClassLoader extends ClassLoader {

//    @Override
//    protected Class<?> findClass(String name) throws ClassNotFoundException {
//
//
//    }


    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
//        String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
        String fileName = "MyString.class";
        InputStream stream = getClass().getResourceAsStream(fileName);
        if (stream == null) {
            return super.loadClass(name);
        }
        try {
            byte[] b = new byte[stream.available()];
            // 将流写入字节数组b中
            int i = stream.read(b);
            return defineClass(name, b, 0, b.length);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return super.loadClass(name);
    }

}
